﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Extensions;
using HiShing.TongDanGuanJia.DeviceDataHistories;
using HiShing.TongDanGuanJia.OnNetaApis.Dto;
using HiShing.TongDanGuanJia.OnNetaApis.Dto.ABP;
using HiShing.TongDanGuanJia.OnNetaApis.Dto.OneNet;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HiShing.TongDanGuanJia.OnNetaApis
{
    [AbpAuthorize]
    public class OneNetApiService : ApplicationService, IOneNetApiService
    {
        private string url = ConfigHelper.GetAppSetting("App", "OneNetServiceUrl");
        private string appkey = ConfigHelper.GetAppSetting("App", "OneNetAPIKey");//您在OneNET平台的APIKey

        private string historyUrl = ConfigHelper.GetAppSetting("App", "HistoryServiceUrl");

        public async Task<DataTableSource<DevicePropertyCurrentValue>> GetDeviceCurrentValues(string deviceId, int deviceTypeId)
        {
            var options = new RestClientOptions(url)
            {
                MaxTimeout = -1,
            };
            var client = new RestClient(options);
            var request = new RestRequest($"{url}/devices/{deviceId}/datastreams", Method.Get);
            request.AddHeader("api-key", appkey);
            RestResponse response = await client.ExecuteAsync(request);

            var content = JsonConvert.DeserializeObject<ResponseContent<List<DevicePropertyCurrentValue>>>(response.Content);
            var dataList = content.data.Where(d => d.id.StartsWith("data")).ToList();

            var result = new DataTableSource<DevicePropertyCurrentValue> { Rows = new List<TableRow<DevicePropertyCurrentValue>> { } };

            for (var i = 0; i < dataList.Count; i++)
            {
                var row = new TableRow<DevicePropertyCurrentValue> { Columns = new List<DevicePropertyCurrentValue>() { } };

                var item1 = dataList.FirstOrDefault(d => d.id == "data" + i);

                if (i == 0)
                {
                    result.LastUpdateTime = item1.update_at;
                    if (deviceTypeId == (int)DeviceTypeEnum.激光数蛋器)
                    {
                        result.TotalCount = long.Parse(item1.current_value) / 65536;
                        continue;
                    }
                }

                item1.sort = int.Parse(item1.id.Substring(4));
                // 视觉数蛋器 data0表示鸡舍1 以此类推
                // 激光数蛋器 data0表示总数， data1表示鸡舍1
                if (deviceTypeId == (int)DeviceTypeEnum.视觉数蛋器)
                {
                    item1.displayName = "鸡舍" + (item1.sort + 1);
                    result.TotalCount += long.Parse(item1.current_value);
                }
                else
                {
                    item1.displayName = "鸡舍" + item1.sort;
                }
                row.Columns.Add(item1);

                i += 1;
                if (i < dataList.Count)
                {
                    var item2 = dataList.FirstOrDefault(d => d.id == "data" + i);
                    item2.sort = int.Parse(item2.id.Substring(4));
                    // 视觉数蛋器 data0表示鸡舍1 以此类推
                    // 激光数蛋器 data0表示总数， data1表示鸡舍1
                    if (deviceTypeId == (int)DeviceTypeEnum.视觉数蛋器)
                    {
                        item2.displayName = "鸡舍" + (item2.sort + 1);
                        result.TotalCount += long.Parse(item2.current_value);
                    }
                    else
                    {
                        item2.displayName = "鸡舍" + item2.sort;
                    }
                    row.Columns.Add(item2);
                }
                result.Rows.Add(row);
            }

            return result;
        }

        public async Task<DeviceInfo> GetDeviceInfo(string deviceId)
        {
            var options = new RestClientOptions(url)
            {
                MaxTimeout = -1,
            };
            var client = new RestClient(options);

            var requestUrl = $"{url}/devices/{deviceId}";

            var request = new RestRequest(requestUrl, Method.Get);
            request.AddHeader("api-key", appkey);
            RestResponse response = await client.ExecuteAsync(request);
            var content = JsonConvert.DeserializeObject<ResponseContent<DeviceInfo>>(response.Content);

            return content.data;
        }

        public async Task<List<Datastream>> GetDeviceProperties(string deviceId, int deviceTypeId)
        {
            var device = await GetDeviceInfo(deviceId);

            var result = new List<Datastream>
            {
                new Datastream { id = "-1", displayName = "全部", sort = -1 }
            };

            if (deviceTypeId == (int)DeviceTypeEnum.视觉数蛋器)
            {
                var items = device.datastreams.Where(s => s.id.StartsWith("data"))
                    .Select(s => new Datastream { id = s.id, sort = int.Parse(s.id.Substring(4)), displayName = "鸡舍" + (int.Parse(s.id.Substring(4)) + 1) })
                    .OrderBy(s => s.sort).ToList();
                result.AddRange(items);
            }
            else
            {
                var items = device.datastreams.Where(s => s.id.StartsWith("data") && s.id != "data0")
                    .Select(s => new Datastream { id = s.id, sort = int.Parse(s.id.Substring(4)), displayName = s.id.Replace("data", "鸡舍") })
                    .OrderBy(s => s.sort).ToList();
                result.AddRange(items);
            }
            return result;
        }


        public async Task<DataTableSource<DevicePropertyHistoryValue>> GetDeviceHistoryValuesPerMinute(GetDeviceHistoryDataDto dto)
        {
            var options = new RestClientOptions(url)
            {
                MaxTimeout = -1,
            };

            var device = await GetDeviceInfo(dto.DeviceId);

            var client = new RestClient(options);

            var requestUrl = $"{url}/devices/{dto.DeviceId}/datapoints?start={dto.StartDate.ToString("yyyy-MM-ddTHH:mm:ss")}" +
                $"&end={dto.EndDate.ToString("yyyy-MM-ddTHH:mm:ss")}&limit={device.datastreams.Count * 12}";

            if (!dto.Cursor.IsNullOrEmpty())
            {
                requestUrl += $"&cursor={dto.Cursor}";
            }

            if (!dto.Sort.IsNullOrEmpty() && dto.Sort.ToUpper() == "DESC")
            {
                requestUrl += $"&sort=DESC";
            }
            var request = new RestRequest(requestUrl, Method.Get);
            request.AddHeader("api-key", appkey);
            RestResponse response = await client.ExecuteAsync(request);
            var content = JsonConvert.DeserializeObject<ResponseContent<DataHistoryResultDto>>(response.Content);
            var dataList = content.data.datastreams.Where(d => d.id.StartsWith("data")).ToList();


            var result = new DataTableSource<DevicePropertyHistoryValue>
            {
                Rows = new List<TableRow<DevicePropertyHistoryValue>> { },
                cursor = content.data.cursor
            };
            var columns = new List<DevicePropertyHistoryValue>() { };
            //提取总数
            var totalItem = new DevicePropertyHistoryValue() { displayName = "产蛋总数", id = "-1", datapoints = new List<Datapoint>() };
            columns.Add(totalItem);

            for (var i = 0; i < dataList.Count; i++)
            {
                var item = dataList.FirstOrDefault(d => d.id == "data" + i);

                item.sort = int.Parse(item.id.Substring(4));
                // 视觉数蛋器 data0表示鸡舍1 以此类推
                // 激光数蛋器 data0表示总数， data1表示鸡舍1
                if (dto.DeviceTypeId == (int)DeviceTypeEnum.视觉数蛋器)
                {
                    item.displayName = "鸡舍" + (item.sort + 1);

                    //累计总数
                    foreach (var point in item.datapoints)
                    {
                        var pointTime = DateTime.Parse(point.at).ToString("yyyy-MM-dd HH:mm:ss");
                        var exist = totalItem.datapoints.FirstOrDefault(t => t.at == pointTime);
                        if (exist == null)
                        {
                            totalItem.datapoints.Add(new Datapoint { at = pointTime, value = point.value });
                        }
                        else
                        {
                            exist.value = (long.Parse(exist.value) + long.Parse(point.value)).ToString();
                        }
                    }
                    columns.Add(item);
                }
                else
                {
                    if (i == 0)
                    {
                        totalItem.datapoints = item.datapoints.Select(d => new Datapoint { at = DateTime.Parse(d.at).ToString("yyyy-MM-dd HH:mm:ss"), value = (long.Parse(d.value) / 65536).ToString() }).ToList();
                    }
                    else
                    {
                        item.displayName = "鸡舍" + item.sort;
                        columns.Add(item);
                    }
                }
            }
            result.Rows.Add(new TableRow<DevicePropertyHistoryValue> { Columns = columns });
            return result;
        }


        public async Task<DataTableSource<DevicePropertyHistoryValue>> GetDeviceHistoryValuesPerDay(GetDeviceHistoryDataDto dto)
        {
            var device = GetDeviceInfo(dto.DeviceId);
            var properties = device.Result.datastreams.Where(s => s.id.StartsWith("data")).ToList();

            var options = new RestClientOptions(historyUrl)
            {
                MaxTimeout = -1,
            };
            var client = new RestClient(options);

            var url = $"{historyUrl}/api/services/app/DeviceDataHistory/GetAll?DeviceId={dto.DeviceId}&StartDate={dto.StartDate}&EndDate={dto.EndDate}" +
                $"&SkipCount={dto.PageIndex * dto.PageSize}&MaxResultCount={dto.PageSize}";

            var request = new RestRequest(url, Method.Get);
            //request.AddHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjYwMTMyIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InJ6ZHQiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9lbWFpbGFkZHJlc3MiOiLml6Xnhaflvrfms7BAaGlzaGluZy5jb20iLCJBc3BOZXQuSWRlbnRpdHkuU2VjdXJpdHlTdGFtcCI6IkFJNlVZNTI3VjZVVEJGSU9aTExVRTdQTU5GVkROUUQzIiwiaHR0cDovL3d3dy5hc3BuZXRib2lsZXJwbGF0ZS5jb20vaWRlbnRpdHkvY2xhaW1zL3RlbmFudElkIjoiMSIsInN1YiI6IjYwMTMyIiwianRpIjoiNTY3Y2NmMzctNzg0Yy00MzM3LWEwN2MtMWI5OTkyYjZmMjlhIiwiaWF0IjoxNzAwNDY4NTU1LCJuYmYiOjE3MDA0Njg1NTUsImV4cCI6MTcwMDU1NDk1NSwiaXNzIjoiT25lTmV0U2VydmljZSIsImF1ZCI6Ik9uZU5ldFNlcnZpY2UifQ.JIsC-6kq2cd54T_AIL91inPRQHpONVKGSEadr9cs6KI");
            RestResponse response = await client.ExecuteAsync(request);
            var histories = JsonConvert.DeserializeObject<ABPResponseContent<PagedResultDto<DeviceDataHistory>>>(response.Content);

            var result = new DataTableSource<DevicePropertyHistoryValue> { Rows = new List<TableRow<DevicePropertyHistoryValue>>() };
            var row = new TableRow<DevicePropertyHistoryValue> { Columns = new List<DevicePropertyHistoryValue>() };
            var columns = new List<DevicePropertyHistoryValue>() { };
            //提取总数
            var totalItem = new DevicePropertyHistoryValue() { displayName = "产蛋总数", id = "-1", datapoints = new List<Datapoint>() };
            columns.Add(totalItem);

            for (var i = 0; i < properties.Count; i++)
            {
                var property = properties.FirstOrDefault(p => p.id == "data" + i);

                var sort = int.Parse(property.id.Substring(4));

                var column = new DevicePropertyHistoryValue();
                // 视觉数蛋器 data0表示鸡舍1 以此类推
                // 激光数蛋器 data0表示总数， data1表示鸡舍1
                if (dto.DeviceTypeId == (int)DeviceTypeEnum.视觉数蛋器)
                {
                    column = new DevicePropertyHistoryValue()
                    {
                        datapoints = getPointValues(histories.result.Items.ToList(), sort),
                        displayName = "鸡舍" + (sort + 1),
                        sort = sort
                    };
                    columns.Add(column);
                    //遍历求总数
                    foreach (var point in column.datapoints)
                    {
                        var pointTime = DateTime.Parse(point.at).ToString("yyyy-MM-dd");
                        var exist = totalItem.datapoints.FirstOrDefault(t => t.at == pointTime);
                        if (exist == null)
                        {
                            totalItem.datapoints.Add(new Datapoint { at = pointTime, value = point.value });
                        }
                        else
                        {
                            exist.value = (long.Parse(exist.value) + long.Parse(point.value)).ToString();
                        }
                    }
                }
                else
                {
                    if (i == 0)
                    {
                        //totalItem.datapoints = getPointValues(histories.result.Items.ToList(), sort);
                        totalItem.datapoints = histories.result.Items.Select(h => new Datapoint { value = (h.Column1 / 65536).ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();
                    }
                    else
                    {
                        column = new DevicePropertyHistoryValue()
                        {
                            datapoints = getPointValues(histories.result.Items.ToList(), sort),
                            displayName = "鸡舍" + sort,
                            sort = sort
                        };
                        columns.Add(column);
                    }
                }
            }
            row.Columns = columns;
            result.Rows.Add(row);
            return result;
        }

        public async Task<List<Datapoint>> GetOneWeekDeviceTotalData(string deviceId, int deviceTypeId, int index, long henCount = 0)
        {
            var histories = await GetDeviceHistoryValuesPerDay(new GetDeviceHistoryDataDto
            {
                PageIndex = 0,
                PageSize = 10000,
                EndDate = DateTime.Today,
                StartDate = DateTime.Today.AddDays(-7),
                DeviceId = deviceId,
                DeviceTypeId = deviceTypeId
            });
            if (henCount > 0)
            {
                return histories.Rows[0].Columns[index].datapoints.Select(p => new Datapoint
                {
                    at = p.at,
                    value = p.value,
                    rate = (float.Parse(p.value) / henCount).ToString("0.00")
                }).OrderBy(p => p.at).ToList();
            }

            return histories.Rows[0].Columns[index].datapoints.OrderBy(p => p.at).ToList();
        }

        #region 私有方法

        private List<Datapoint> getPointValues(List<DeviceDataHistory> histories, int index)
        {

            switch (index)
            {
                case 0:
                    return histories.Select(h => new Datapoint { value = h.Column1.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();
                case 1:
                    return histories.Select(h => new Datapoint { value = h.Column2.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();
                case 2:
                    return histories.Select(h => new Datapoint { value = h.Column3.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 3:
                    return histories.Select(h => new Datapoint { value = h.Column4.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 4:
                    return histories.Select(h => new Datapoint { value = h.Column5.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 5:
                    return histories.Select(h => new Datapoint { value = h.Column6.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 6:
                    return histories.Select(h => new Datapoint { value = h.Column7.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 7:
                    return histories.Select(h => new Datapoint { value = h.Column8.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 8:
                    return histories.Select(h => new Datapoint { value = h.Column9.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 9:
                    return histories.Select(h => new Datapoint { value = h.Column10.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 10:
                    return histories.Select(h => new Datapoint { value = h.Column11.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 11:
                    return histories.Select(h => new Datapoint { value = h.Column12.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 12:
                    return histories.Select(h => new Datapoint { value = h.Column13.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 13:
                    return histories.Select(h => new Datapoint { value = h.Column14.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 14:
                    return histories.Select(h => new Datapoint { value = h.Column15.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 15:
                    return histories.Select(h => new Datapoint { value = h.Column16.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 16:
                    return histories.Select(h => new Datapoint { value = h.Column17.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 17:
                    return histories.Select(h => new Datapoint { value = h.Column18.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 18:
                    return histories.Select(h => new Datapoint { value = h.Column19.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 19:
                    return histories.Select(h => new Datapoint { value = h.Column20.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 20:
                    return histories.Select(h => new Datapoint { value = h.Column21.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 21:
                    return histories.Select(h => new Datapoint { value = h.Column22.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 22:
                    return histories.Select(h => new Datapoint { value = h.Column23.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 23:
                    return histories.Select(h => new Datapoint { value = h.Column24.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 24:
                    return histories.Select(h => new Datapoint { value = h.Column25.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 25:
                    return histories.Select(h => new Datapoint { value = h.Column26.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 26:
                    return histories.Select(h => new Datapoint { value = h.Column27.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 27:
                    return histories.Select(h => new Datapoint { value = h.Column28.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 28:
                    return histories.Select(h => new Datapoint { value = h.Column29.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 29:
                    return histories.Select(h => new Datapoint { value = h.Column30.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 30:
                    return histories.Select(h => new Datapoint { value = h.Column31.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 31:
                    return histories.Select(h => new Datapoint { value = h.Column32.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();


                case 32:
                    return histories.Select(h => new Datapoint { value = h.Column33.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 33:
                    return histories.Select(h => new Datapoint { value = h.Column34.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 34:
                    return histories.Select(h => new Datapoint { value = h.Column35.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 35:
                    return histories.Select(h => new Datapoint { value = h.Column36.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();


                case 36:
                    return histories.Select(h => new Datapoint { value = h.Column37.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 37:
                    return histories.Select(h => new Datapoint { value = h.Column38.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 38:
                    return histories.Select(h => new Datapoint { value = h.Column39.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 39:
                    return histories.Select(h => new Datapoint { value = h.Column40.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 40:
                    return histories.Select(h => new Datapoint { value = h.Column41.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 41:
                    return histories.Select(h => new Datapoint { value = h.Column42.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 42:
                    return histories.Select(h => new Datapoint { value = h.Column43.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 43:
                    return histories.Select(h => new Datapoint { value = h.Column44.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 44:
                    return histories.Select(h => new Datapoint { value = h.Column45.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 45:
                    return histories.Select(h => new Datapoint { value = h.Column46.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 46:
                    return histories.Select(h => new Datapoint { value = h.Column47.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 47:
                    return histories.Select(h => new Datapoint { value = h.Column48.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 48:
                    return histories.Select(h => new Datapoint { value = h.Column49.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 49:
                    return histories.Select(h => new Datapoint { value = h.Column50.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 50:
                    return histories.Select(h => new Datapoint { value = h.Column51.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();


                case 51:
                    return histories.Select(h => new Datapoint { value = h.Column52.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 52:
                    return histories.Select(h => new Datapoint { value = h.Column53.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 53:
                    return histories.Select(h => new Datapoint { value = h.Column54.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 54:
                    return histories.Select(h => new Datapoint { value = h.Column55.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 55:
                    return histories.Select(h => new Datapoint { value = h.Column56.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();


                case 56:
                    return histories.Select(h => new Datapoint { value = h.Column57.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 57:
                    return histories.Select(h => new Datapoint { value = h.Column58.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 58:
                    return histories.Select(h => new Datapoint { value = h.Column59.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 59:
                    return histories.Select(h => new Datapoint { value = h.Column60.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 60:
                    return histories.Select(h => new Datapoint { value = h.Column61.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 61:
                    return histories.Select(h => new Datapoint { value = h.Column62.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 62:
                    return histories.Select(h => new Datapoint { value = h.Column63.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 63:
                    return histories.Select(h => new Datapoint { value = h.Column64.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 64:
                    return histories.Select(h => new Datapoint { value = h.Column65.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 65:
                    return histories.Select(h => new Datapoint { value = h.Column66.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 66:
                    return histories.Select(h => new Datapoint { value = h.Column67.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 67:
                    return histories.Select(h => new Datapoint { value = h.Column68.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 68:
                    return histories.Select(h => new Datapoint { value = h.Column69.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 69:
                    return histories.Select(h => new Datapoint { value = h.Column70.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 70:
                    return histories.Select(h => new Datapoint { value = h.Column71.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 71:
                    return histories.Select(h => new Datapoint { value = h.Column72.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();


                case 72:
                    return histories.Select(h => new Datapoint { value = h.Column73.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 73:
                    return histories.Select(h => new Datapoint { value = h.Column74.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 74:
                    return histories.Select(h => new Datapoint { value = h.Column75.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 75:
                    return histories.Select(h => new Datapoint { value = h.Column76.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();


                case 76:
                    return histories.Select(h => new Datapoint { value = h.Column77.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 77:
                    return histories.Select(h => new Datapoint { value = h.Column78.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 78:
                    return histories.Select(h => new Datapoint { value = h.Column79.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 79:
                    return histories.Select(h => new Datapoint { value = h.Column80.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 80:
                    return histories.Select(h => new Datapoint { value = h.Column81.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 81:
                    return histories.Select(h => new Datapoint { value = h.Column82.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 82:
                    return histories.Select(h => new Datapoint { value = h.Column83.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 83:
                    return histories.Select(h => new Datapoint { value = h.Column84.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 84:
                    return histories.Select(h => new Datapoint { value = h.Column85.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 85:
                    return histories.Select(h => new Datapoint { value = h.Column86.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 86:
                    return histories.Select(h => new Datapoint { value = h.Column87.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 87:
                    return histories.Select(h => new Datapoint { value = h.Column88.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 88:
                    return histories.Select(h => new Datapoint { value = h.Column89.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 89:
                    return histories.Select(h => new Datapoint { value = h.Column90.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 90:
                    return histories.Select(h => new Datapoint { value = h.Column91.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 91:
                    return histories.Select(h => new Datapoint { value = h.Column92.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 92:
                    return histories.Select(h => new Datapoint { value = h.Column93.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 93:
                    return histories.Select(h => new Datapoint { value = h.Column94.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 94:
                    return histories.Select(h => new Datapoint { value = h.Column95.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 95:
                    return histories.Select(h => new Datapoint { value = h.Column96.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 96:
                    return histories.Select(h => new Datapoint { value = h.Column97.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 97:
                    return histories.Select(h => new Datapoint { value = h.Column98.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 98:
                    return histories.Select(h => new Datapoint { value = h.Column99.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 99:
                    return histories.Select(h => new Datapoint { value = h.Column100.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();

                case 100:
                    return histories.Select(h => new Datapoint { value = h.Column101.ToString(), at = h.AcquisitionTime.ToString("yyyy-MM-dd") }).ToList();
            }
            return new List<Datapoint> { new Datapoint() { } };
        }

        #endregion
    }
}
